﻿using GloboTicketAdmin.API.Models;
using GloboTicketAdmin.API.Models.Domain;
using SqlSugar;

namespace SqlSugarMultiDb.Common;

/// <summary>
/// CodeFirst初始化器
/// </summary>
public class CodeFirstInitializer
{
	private readonly ISqlSugarClient _db;
	private readonly ILogger<CodeFirstInitializer> logger;

	public CodeFirstInitializer(ISqlSugarClient db, ILogger<CodeFirstInitializer> logger)
	{
		_db = db;
		this.logger = logger;
	}

	/// <summary>
	/// 初始化数据库表结构
	/// </summary>
	public void Init()
	{
		logger.LogInformation("开始初始化数据库表结构...");
		// 创建数据库（如果不存在）
		_db.DbMaintenance.CreateDatabase();

		// 创建或更新表结构
		_db.CodeFirst.InitTables(typeof(CategoryModel), typeof(EventModel));

		// 种子数据（初始化默认数据）
		SeedData();
	}

	/// <summary>
	/// 初始化种子数据
	/// </summary>
	private void SeedData()
	{
		if (!_db.Queryable<CategoryModel>().Any())
		{
			// 添加默认用户
			var defaultCategoryModels = new List<CategoryModel>
			{
				new CategoryModel { Name = "演唱会",Lable="concert" },
				new CategoryModel { Name = "体育赛事" ,Lable = "sports"},
				new CategoryModel{Name="电影" ,Lable = "movie"},
				new CategoryModel{Name="话剧",Lable = "drama" },
				new CategoryModel{Name="音乐剧",Lable= "musical" },
				new CategoryModel{Name="展览" ,Lable = "exhibition"},
				new CategoryModel{Name="游乐园",Lable = "park" },
				new CategoryModel{Name="其他",Lable = "others" }
			};

			_db.Insertable(defaultCategoryModels).ExecuteCommand();
		}

		if (!_db.Queryable<EventModel>().Any())
		{
			var list = _db.Queryable<CategoryModel>().ToList();
			var defaultEventModels = new List<EventModel>
			{
				new EventModel
				{
					Name = "周杰伦演唱会",
					StartTime = new DateTime(2024,3,15,19,30,0),
					EndTime = new DateTime(2024,3,15,22,0,0),
					Price = 380.00,
					Stock = 1000,
					Artists = new List<string> { "周杰伦" },
					Description = "2024周杰伦嘉年华世界巡回演唱会门票，包含经典曲目演唱，豪华舞台效果，互动环节等精彩内容。演出时长约150分钟，提前1小时入场。",
					ImageUrl = "https://picsum.photos/seed/concert1/300/400",
					EventStatus = EventStatusModel.OnSale,
					CategoryId = list.Find(c => c.Name == "演唱会")!.Id
				},
				new EventModel
				{
					Name = "NBA季后赛门票",
					StartTime = new DateTime(2024,4,10,10,30,0),
					EndTime = new DateTime(2024,4,10,13,30,0),
					Price = 880.00,
					Stock = 50,
					Artists = new List<string> { "勒布朗·詹姆斯", "库里" },
					Description = "2024 NBA季后赛门票，湖人VS勇士巅峰对决，感受顶级篮球赛事的魅力。场馆提供餐饮服务，禁止携带专业相机入场。",
					ImageUrl = "https://picsum.photos/seed/basketball1/300/400",
					EventStatus = EventStatusModel.OnSale,
					CategoryId = list.Find(c => c.Name == "体育赛事")!.Id
				},
				 new EventModel
				{
					Name = "复仇者联盟5首映",
					StartTime = new DateTime(2024,5,1),
					EndTime = new DateTime(2024,5,1,23,0,0),
					Price = 120.00,
					Stock = 200,
					Artists = new List<string> { "小罗伯特·唐尼", "克里斯·埃文斯" },
					Description = "复仇者联盟5：终局之战首映IMAX电影票，漫威宇宙最新力作，震撼视觉效果。IMAX厅提供最佳观影体验，提前30分钟入场。",
					ImageUrl ="https://picsum.photos/seed/movie1/300/400",
					EventStatus = EventStatusModel.OnSale,
					CategoryId = list.Find(c => c.Name == "电影")!.Id
				},
				  new EventModel
				{
					Name = "上海迪士尼一日票",
					StartTime = new DateTime(2024,6,15,8,0,0),
					EndTime = new DateTime(2024,6,15,22,0,0),
					Price = 599.00,
					Stock = 500,
					Artists = new List<string> { "米老鼠","唐老鸭" },
					Description = "上海迪士尼乐园一日门票，含所有游乐设施，七大主题园区任您探索。推荐提前规划行程，热门项目可能需要排队，夏季注意防晒。",
					ImageUrl = "https://picsum.photos/seed/themepark1/300/400",
					EventStatus = EventStatusModel.OnSale,
					CategoryId = list.Find(c => c.Name == "游乐园")!.Id
				},
				   new EventModel
				{
					Name = "国家大剧院音乐会",
					StartTime = new DateTime(2024,7,20,19,0,0),
					EndTime = new DateTime(2024,7,20,22,0,0),
					Price = 480.00,
					Stock = 300,
					Artists = new List<string> { "贝多芬","国家交响乐团" },
					Description = "贝多芬第九交响曲音乐会门票，由著名指挥家带领国家交响乐团现场演奏，感受古典音乐的魅力。场馆内禁止携带饮料和食物。",
					ImageUrl = "https://picsum.photos/seed/concert2/300/400",
					EventStatus = EventStatusModel.OnSale,
					CategoryId = list.Find(c => c.Name == "音乐剧")!.Id
				},

			};
			_db.Insertable(defaultEventModels).ExecuteCommand();
		}
	}
}
